<?php

namespace App\Admin\Controllers;

use App\Models\HostInfo;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

use Encore\Admin\Facades\Admin;

class HostController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '主机管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new HostInfo);

        $grid->column('id', __('Id'));
        $grid->column('name', __('名称'));
        $grid->column('desc', __('描述'));
        $grid->column('ip', __('IP地址'));
        $grid->column('cmd', __('控制命令'));
        $grid->column('sys', __('操作系统'));
      
        $grid->column('status', __('状态'))->using(['0' => '<span style="color:red">已关机</span>', '1' => '<span style="color:green">运行中</span>'], '未知');
        $grid->column('enable', __('启用'))->using(['0' => '<span style="color:red">未启用</span>', '1' => '<span style="color:green">已启用</span>'], '未知');
        $grid->column(' ', __('关机'))->display(function () {
            return "<a class='btn btn-danger btn-sm host-row-off' data-id='{$this}'>关机</a>";
        });

        $this->script = <<<EOT
        //open all
        $('.host-row-off').unbind('click').click(function() {
            console.log($(this).data('id'));
            var _id = $(this).data('id').id;
            var _sys = $(this).data('id').sys;
            var _name = $(this).data('id').name;
            var _ip = $(this).data('id').ip;
            var _cmd = $(this).data('id').cmd;
            swal({
                title: "确认关机" + _name + "吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {

                    $.ajax({
                        url: 'http://' + _ip + ':8001',
                        type: 'POST',
                        dataType: 'jsonp',  //数据格式为jsonp
                        jsonp: 'callback',  //函数变量名
                        jsonpCallback: 'list',
                        success:function(data){  //成功的回调函数
                            console.log(data);
                            var _swal = swal({
                              title: "正在关机，请不要关闭浏览器!",
                              type: "warning",
                            });
                            setTimeout(function() {
                                $.ajax({
                                    method: 'get',
                                    url: '/api/checkHost?ip=' + _ip + '&id=' + _id,
                                    success: function (data) {
                                        console.log(data);
                                        if(data == 0 ) {
                                            swal({
                                              title: _name + "电脑关机成功!",
                                              type: "success",
                                            });
                                        } else {
                                            swal({
                                              title: _name + "电脑关机失败!",
                                              type: "error",
                                            });
                                        }
                                        $.pjax.reload('#pjax-container');
                                    }
                                });
                            }, 10000);
                        },
                        error: function (e) {
                            console.log(e)
                        }
                    });


                    // $.ajax({
                    //     method: 'get',
                    //     url: '/api/hostShutdown?id=' + _id + '&sys=' + _sys + '&ip=' + _ip + '&cmd=' + _cmd,
                    //     success: function (data) {
                    //         // console.log(data);
                    //         var o = eval("(" + data + ")");  // 转换为json对象
                    //         console.log(o.data);
                    //         var _swal = swal({
                    //           title: "正在关机，请不要关闭浏览器!",
                    //           type: "warning",
                    //         });

                    //         setTimeout(function() {
                    //             $.ajax({
                    //                 method: 'get',
                    //                 url: '/api/checkHost?ip=' + _ip + '&id=' + _id,
                    //                 success: function (data) {
                    //                     console.log(data);
                    //                     if(data == 0 ) {
                    //                         swal({
                    //                           title: _name + "电脑关机成功!",
                    //                           type: "success",
                    //                         });
                    //                     } else {
                    //                         swal({
                    //                           title: _name + "电脑关机失败!",
                    //                           type: "error",
                    //                         });
                    //                     }
                    //                     $.pjax.reload('#pjax-container');
                    //                 }
                    //             });
                    //         }, 10000);
                    //     }
                    // });
                }
            });
        });
EOT;
        Admin::script($this->script);
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(HostInfo::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('名称'));
        $show->field('desc', __('描述'));
        $show->field('ip', __('Ip'));
        $show->field('cmd', __('控制命令'));
        $show->field('sys', __('操作系统'));
        $show->field('enable', __('启用状态'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new HostInfo);

        $form->text('name', __('名称'));
        $form->text('desc', __('描述'));
        $form->ip('ip', __('Ip'));
        $form->text('cmd', __('控制命令'));
        $form->text('sys', __('操作系统'));
        // $form->number('enable', __('Enable'));
        $directors = [
            0 => '未启用',
            1 => '已启用'
        ];

        $form->select('enable', '启用状态')->options($directors);

        return $form;
    }
}
